//
//  DDMeViewController.m
//  DDMusic
//
//  Created by rimi on 16/11/10.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//

#import "DDMeViewController.h"
#import "LCBlur.h"
#import <Masonry.h>
#import "MyScrollView.h"
#import "UILabel+DDUILabelCategory.h"
#import "DDSetTheController.h"
#import "DDFansController.h"
#import "DDFocusOnController.h"
#import "DDFriendsController.h"
#import "DDCollectionController.h"
#import "DDSystemMessageController.h"
#import "DDThePlaylistController.h"
#import "DDMeCell.h"
#import "DDTheBackgroundImageController.h"
#import "DDScanController.h"
#import "DDSharePlatformView.h"

@interface DDMeViewController ()
@property(nonatomic,strong)UIView* SetView;
@property (nonatomic,strong)UIImageView* Backgroundimage;
@property (nonatomic,strong) NSTimer* timer;

@end


BOOL is = NO;
@implementation DDMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self registerCenter];
    self.navigationItem.title = @"个人中心";
    self.timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(imageAnimation) userInfo:nil repeats:YES];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self MeBackgroundUI];
    [self imageAnimation];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
//设置UI
- (void)MeBackgroundUI {
    
    //背景View
    UIView* backgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 280)];
    
    
    //更多
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"MoreAndMore"] style:UIBarButtonItemStylePlain target:self action:@selector(MultipleChoice)];
    
    
    //显示信息的View
    UIView* view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ChangeTheBackgroundImage)]];
    UIView* Backgroundview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];

   self.Backgroundimage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 400)];
    //背景图片
    self.Backgroundimage.image = [UIImage imageNamed:@"simage"];
    self.Backgroundimage.userInteractionEnabled = YES;

    
    
    [Backgroundview blurWithRadius:2.0f];
    [self.tableView addSubview:self.Backgroundimage];
    
    [view addSubview:self.Backgroundimage];
    [view addSubview:Backgroundview];
    view.layer.masksToBounds = YES;
    
    [backgView addSubview:view];
    

   

    
    
    //头像
    UIButton* HeadPortraitImage = [[UIButton alloc]init];
    HeadPortraitImage.backgroundColor = [UIColor redColor];
    HeadPortraitImage.layer.cornerRadius = 35;
    [HeadPortraitImage setImage:[UIImage imageNamed:@"HeadImage-1"] forState:UIControlStateNormal];
    HeadPortraitImage.layer.masksToBounds = YES;
    
    //昵称
    UILabel* TheNameLabel = [[UILabel alloc]init];
    TheNameLabel.text = @"勿念";
    TheNameLabel.textColor = [UIColor whiteColor];
    TheNameLabel.font = [UIFont systemFontOfSize:20];
    
    //性别
    UIImageView* genderImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"male"]];
    
    
    //年龄
    UILabel* AgeLabel = [[UILabel alloc]init];
    AgeLabel.text = @"22岁";
    AgeLabel.textColor = [UIColor lightGrayColor];
    AgeLabel.font = [UIFont systemFontOfSize:15];
    
    //地区
    UILabel* RegionLabel = [[UILabel alloc]init];
    RegionLabel.text = @"四川南充";
    RegionLabel.textColor = [UIColor lightGrayColor];
    RegionLabel.font = [UIFont systemFontOfSize:15];
    
    
    //作品
    UIButton* works = [[UIButton alloc]init];
    [works setTitle:@"作品" forState:UIControlStateNormal];
    [works setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [works addTarget:self action:@selector(pointworksThe) forControlEvents:UIControlEventTouchUpInside];
    //粉丝
    UIButton* fans = [[UIButton alloc]init];
    [fans setTitle:@"粉丝" forState:UIControlStateNormal];
    [fans setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [fans addTarget:self action:@selector(pointfansThe) forControlEvents:UIControlEventTouchUpInside];
    //关注
    UIButton* FocusOn = [[UIButton alloc]init];
    [FocusOn setTitle:@"关注" forState:UIControlStateNormal];
    [FocusOn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [FocusOn addTarget:self action:@selector(PointFocusOnThe) forControlEvents:UIControlEventTouchUpInside];
    //好友
    UIButton* friends = [[UIButton alloc]init];
    [friends setTitle:@"好友" forState:UIControlStateNormal];
    [friends setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [friends addTarget:self action:@selector(PointfriendsThe) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    

    
    NSArray* btnImageArr = @[@"directory",@"collection",@"history",@"xiMessage"];
    NSArray* btnTitleArr = @[@"我的歌单",@"我的收藏",@"播放历史",@"系统消息"];

    //添加四个点击按钮
    NSInteger btnHeight = 0.1*SCREEN_SIZE.height - 35;//按钮的宽高
    NSInteger btnPitch = (SCREEN_SIZE.width-btnHeight*3) / 5;//按钮之间的间距
    for (NSInteger i = 0; i < 4; i++) {
        UIButton* btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        [btn setBackgroundImage:[UIImage imageNamed:btnImageArr[i]] forState:(UIControlStateNormal)];
        btn.tag = 200+i;
        
        //点击事件
        [btn addTarget:self action:@selector(AddplaylistThe:) forControlEvents:UIControlEventTouchUpInside];
        
        [backgView addSubview:btn];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        UILabel* label = [UILabel dd_CreatLabelWithTitle:btnTitleArr[i] font:16 TitleColor:[UIColor blackColor] TextAlignment:(NSTextAlignmentCenter)];
        [backgView addSubview:label];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@(btnHeight));
            make.top.equalTo(@(0.25 * SCREEN_SIZE.height + 20));
            make.left.equalTo(@(35+(btnHeight+btnPitch)*i));
            
            
        }];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btn.mas_bottom).offset(4);
            make.centerX.equalTo(btn.mas_centerX);
        }];
        
        

    }
    
    


    
    [Backgroundview addSubview:FocusOn];
    [Backgroundview addSubview:friends];
    [Backgroundview addSubview:fans];
    [Backgroundview addSubview:works];
    [Backgroundview addSubview:RegionLabel];
    [Backgroundview addSubview:AgeLabel];
    [Backgroundview addSubview:genderImage];
    [Backgroundview addSubview:TheNameLabel];
    [Backgroundview addSubview:HeadPortraitImage];
    //添加头部试图
    self.tableView.tableHeaderView = backgView;
    
    NSArray* arr = @[works,fans,FocusOn,friends];
    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton* button = (UIButton*)obj;
       [obj mas_makeConstraints:^(MASConstraintMaker *make) {
           make.top.equalTo(HeadPortraitImage.mas_bottom).offset(5);
           make.bottom.equalTo(@0);
           make.width.equalTo(@([UIScreen mainScreen].bounds.size.width/4));
           make.left.equalTo(@([UIScreen mainScreen].bounds.size.width/4*idx));
       }];
//        设置分割线
        UIView* view = [[UIView alloc]init];
        view.backgroundColor = [UIColor lightGrayColor];
        [obj addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@8);
            make.bottom.equalTo(@(-8));
            make.right.equalTo(button.mas_right).offset(1);
            make.width.equalTo(@1);
        }];
    }];
    
    
    
    [TheNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(HeadPortraitImage.mas_top).offset(10);
        make.left.equalTo(HeadPortraitImage.mas_right).offset(8);
        
    }];
    
    [HeadPortraitImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@50);
        make.left.equalTo(@16);
        make.width.height.equalTo(@70);
    }];
    
    [genderImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(TheNameLabel.mas_bottom).offset(5);
        make.left.equalTo(HeadPortraitImage.mas_right).offset(8);
        make.width.equalTo(@18);
        make.height.equalTo(@18);
    }];
    
    [AgeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(TheNameLabel.mas_bottom).offset(5);
        make.left.equalTo(genderImage.mas_right).offset(5);
        
    }];
    
    [RegionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(TheNameLabel.mas_bottom).offset(5);
        make.left.equalTo(AgeLabel.mas_right).offset(5);
    }];
    


    
    //更多选项
    _SetView = [[UIView alloc]init];
    UIImageView* image = [[UIImageView alloc]initWithFrame:self.SetView.bounds];
    image.image = [UIImage imageNamed:@"ALotOf"];
    

    
    [Backgroundview addSubview:_SetView];
    
    [self.SetView addSubview:image];
    
    
    [self.SetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@100);
        make.height.equalTo(@120);
        make.top.equalTo(@0);
        make.right.equalTo(@-30);
    }];
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    
    NSArray* SetImage = @[@"SetImage",@"QrCode",@"forwarding"];
    NSArray* SetTitle = @[@"设置",@"扫一扫",@"转发"];
    
    for (NSInteger i = 0; i < 3 ; i ++) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:SetImage[i]] forState:UIControlStateNormal];
        [button setTitle:SetTitle[i] forState:UIControlStateNormal];
        button.tag = 200+i;
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button addTarget:self action:@selector(pointChooseThe:) forControlEvents:UIControlEventTouchUpInside];
        button.imageEdgeInsets = UIEdgeInsetsMake(0, -27, 0, 0);
        if (i == 1) {
            button.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        }else if (i != 1){
            button.titleEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
        }
        [self.SetView addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.SetView.mas_top).offset(15+30*i);
            make.left.equalTo(@0);
            make.right.equalTo(@0);
            make.height.equalTo(@44);
        }];
        
        self.SetView.hidden = YES;
    }
    

}

//背景动画方法
- (void)imageAnimation{
    //设置背景图片动画
    [UIView animateWithDuration:5.0 animations:^{
        self.Backgroundimage.transform
        = CGAffineTransformScale(self.Backgroundimage.transform, 2, 2);
        
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:5.0 animations:^{
            self.Backgroundimage.transform = CGAffineTransformIdentity;
        }];
    }];

}
//更多按钮回调
- (void)MultipleChoice {
    if (is == NO) {
        self.SetView.hidden = NO;
        is = YES;
    }else{
        self.SetView.hidden = YES;
        is = NO;
    }
    
}

//添加歌单栏点击事件
- (void)AddplaylistThe:(UIButton*)button{
    switch (button.tag) {
        case 200:
            NSLog(@"点击了我的歌单");
            break;
        case 201:{
            DDCollectionController* CollectionVc = [DDCollectionController new];
            [self.navigationController pushViewController:CollectionVc animated:YES];
        }
            break;
        case 202:{
            DDThePlaylistController* ThePlayVc = [DDThePlaylistController new];
            [self.navigationController pushViewController:ThePlayVc animated:YES];
            NSLog(@"点击了播放历史");
            break;
        }
            
        default:
            NSLog(@"点击了系统消息");
            DDSystemMessageController* SystemVc = [DDSystemMessageController new];
            [self.navigationController pushViewController:SystemVc animated:YES];
            break;
    }
    
    
}


- (void)pointworksThe {
    NSLog(@"点击了作品按钮");
   
}

- (void)pointfansThe {
    
    DDFansController* fansVc = [[DDFansController alloc]init];
    [self.navigationController pushViewController:fansVc animated:YES];
}

- (void)PointFocusOnThe {
    
    DDFocusOnController* focusVc = [DDFocusOnController new];
    [self.navigationController pushViewController:focusVc animated:YES];
}

- (void)PointfriendsThe {
    NSLog(@"点击了好友按钮");
    DDFriendsController* FridendsVc = [DDFriendsController new];
    [self.navigationController pushViewController:FridendsVc animated:YES];
}

- (void)pointChooseThe:(UIButton *)button {
    switch (button.tag) {
        case 200:{
            DDSetTheController* setVC = [[DDSetTheController alloc]init];
            [self.navigationController pushViewController:setVC animated:YES];
        }
            break;
        case 201:{
            DDScanController* ScanVc = [[DDScanController alloc]init];
            [self.navigationController pushViewController:ScanVc animated:YES];
        }
            break;
        default:{
            NSArray *imageArray = @[@"wechat",@"friendquan",@"qq",@"qqzone",@"weibo"];
            DDSharePlatformView* shareVc = [[DDSharePlatformView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height) ImageArray:imageArray didShareButtonBlock:^(NSInteger tag) {
                NSLog(@"---- %ld",tag);
            }];
            [shareVc show];
        
    }

            break;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DDMeCell* cell = [[NSBundle mainBundle]loadNibNamed:@"DDMeCell" owner:nil options:nil].firstObject;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 50;
            break;
            
        default:
            return 1;
            break;
            
    }
}


- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    switch (section) {
        case 0:
            return @"单曲";
            break;
            
        default:
            return @"";
            break;
    }
}




//
- (void)ChangeTheBackgroundImage {
    DDTheBackgroundImageController* imageVc = [DDTheBackgroundImageController new];
    [self.navigationController pushViewController:imageVc animated:YES];
}

#pragma mark 通知中心
- (void)registerCenter{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dDTheBackgroundImageControllerDismiss:) name:@"DDTheBackgroundImageControllerDismiss" object:nil];
}
#pragma mark 通知回调方法
- (void)dDTheBackgroundImageControllerDismiss:(NSNotification*)notification{
    NSMutableArray* imageArr = (NSMutableArray*)notification.object;
    if (imageArr.count >0) {
        NSLog(@"%@",notification.object);
        
        self.Backgroundimage.image = imageArr.lastObject;
//        self.Backgroundimage.animationDuration = 1.0;
//        //
//        self.Backgroundimage.animationRepeatCount = 0;
//        [self.Backgroundimage startAnimating];
    }
}

@end
